// pages/choiceProject/index.js
import axios from '../network/index'
var pageNum;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showClose: false,
    searchText: '',
    list: [],
    spaceText: "加载中...",
    showMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    pageNum = 1
    this.getMaterialList(pageNum)
  },
  //搜索框事件
  searchIpt(e) {
    var _this = this
    this.setData({
      showClose: e.detail.value ? true : false
    })
    pageNum = 1
    this.getMaterialList(pageNum, e.detail.value)
  },
  //载入缓存
  setCache(list = []) {
    var _this = this
    let cacheList = wx.getStorageSync('cacheList')
    list.forEach(item => {
      cacheList.forEach(items => {
        if (items.materialCode == item.materialCode) {
          item.num = items.num
          item.unitPrice = items.unitPrice
        }
      })
    })
    _this.setData({
      list
    })
  },
  //清空搜索框内容
  clearSearhText() {
    this.setData({
      searchText: '',
      showClose: false
    })
    this.getMaterialList(pageNum)
  },
  //获取科目列表
  getMaterialList(pageNum, searchVal = "") {
    axios({
      url: "/api/material/list",
      method: 'POST',
      data: {
        pageNum,
        pageSize: 10,
        searchVal
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.code == 200) {
        res.data.content.forEach(item => {
          if (!item.num) item['num'] = 0
          if (!item.price) item['price'] = 0.00
        })
        var arr = this.data.list
        if (pageNum > 1) {
          arr.push(...res.data.content)
        } else {
          arr = res.data.content
        }
        this.setData({
          list: arr,
          spaceText: arr && arr.length > 0 ? '' : '暂无数据',
          showMore: false
        })
        this.setCache(arr)
      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false
        })
      }
    }).catch(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    })
  },
  //选择变化事件
  numChange(e) {
    var list = this.data.list
    var index = e.currentTarget.dataset.index
    list[index].num = e.detail;
    this.setData({
      list
    })
    wx.setStorageSync('cacheList', list.filter(item => item.num > 0))
  },

  //确认
  confirm() {
    let cacheList = this.data.list.filter(item => item.num > 0)
    wx.setStorageSync('cacheList', cacheList)
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      showMore: true
    })
    pageNum += 1
    this.getMaterialList(pageNum)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})